﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using trangbaocao.Models;
using System.Collections;

namespace trangbaocao.Controllers
{
    [Authorize]
    public class FuelController : Controller
    {
        //
        // GET: /Fuel/
        
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        //FuelStationsAssociateModel FuelStationsAssociateModel = new FuelStationsAssociateModel();
        //FuelGeneratorAssociateModel FuelGeneratorAssociateModel = new FuelGeneratorAssociateModel();
        //---------------------------------Fuel Stations---------------------------------------------------

        FuelStationsModel FuelStationsModel = new FuelStationsModel();
        public ActionResult Fuel_Stations_Index(string thang)
        {
            string nam = DateTime.Now.Year.ToString();
            string thang_="";
            if (thang.ToString().Length != 0)
                thang_ = thang;
            else
                thang_ = DateTime.Now.Month.ToString();
            TempData["select_thang"] = thang_;
            string str_detail = " and nam=" + nam + " and thang=" + thang_;
            List<FuelStationsModel> FuelStations = FuelStationsModel.getFuelStationsIndex(str_detail);
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            ViewBag.thang = thang;
            return View(FuelStations);
        }
        public ActionResult FuelStationsEdit(int id, int thang)
        {
            string str_detail = "";
            string nam = DateTime.Now.Year.ToString();
            string thang_ = thang.ToString();
            str_detail = " and nam=" + nam + " and thang<=" + thang_;
            FuelStationsModel FuelStationsModel_ = FuelStationsModel.getFuelStationsIndex(str_detail).Find(delegate(FuelStationsModel s) { return s.stations_id == id; });
            FuelStationsModel_.nam = Convert.ToInt32(DateTime.Now.Year.ToString());
            FuelStationsModel_.thang = thang;
            return View(FuelStationsModel_);
        }
        [HttpPost]
        public ActionResult FuelStationsEdit(FuelStationsModel FuelStations)
        {
            if (ModelState.IsValid)
            {
                if (FuelStations.ngay_nhap == "" || FuelStations.ngay_nhap == null)
                    FuelStations.ngay_nhap = DateTime.Today.ToString("dd/MM/yyyy");
                FuelStations.ngay_cap_nhat = DateTime.Today.ToString("dd/MM/yyyy");
                FuelStationsModel.updateFuelStations(FuelStations);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Fuel_Stations_Index", new { thang = FuelStations.thang });
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(FuelStations);
        }

        public ActionResult FindFuelStations()
        {
            return RedirectToAction("Fuel_Stations_Index");
        }
        [HttpPost]
        public ActionResult FindFuelStations(string timkiem)
        {
            string trungtam_id = "";
            int num;
            bool main_stations_id_check = int.TryParse(trungtam_id, out num);
            string s = "";
            if (int.TryParse(Request.Form["main_stations_id"], out num))
            {
                trungtam_id = Request.Form["main_stations_id"];
                s = " c.id=" + trungtam_id + " AND ";
            }
            if (Session != null && s.ToString() == "")
            {
                int quyen_id = Convert.ToInt16(Session["quyen_id"]);
                int trungtam_session_id = Convert.ToInt16(Session["trungtam_id"]);
                if (quyen_id > 2)
                    s = " c.id=" + trungtam_session_id + " AND ";
            }
            string sql;
            string dk = "";
            if (Request["select_option"] == "stations.name")
                dk = "b.name";
            if (timkiem == "")
                sql = s + "(" + dk + " like '%" + timkiem + "%' or " + dk + " is null)";
            else
                sql = s + "(" + dk + " like '%" + timkiem + "%' or upper(" + dk + ") like upper('%" + timkiem + "%'))";
            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];

            int year = DateTime.Now.Year;
            string month = Request["thang_hidden"];
            //if (month == "")
            //    month = DateTime.Now.Month.ToString();
            //if (month.ToString() == "1")
            //{
            //    year = year - 1;
            //    month = "12";
            //}
            //else
            //    month = (Convert.ToInt32(month) - 1).ToString();
            List<FuelStationsModel> FuelStations = FuelStationsModel.FindFuelStations(sql, year.ToString(), month.ToString());
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            TempData["select_thang"] = Request["thang_hidden"];
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            ViewBag.thang = Request["thang_hidden"];
            return View("Fuel_Stations_Index", FuelStations);
        }
        public ActionResult Fuel_Stations_Update(string id)
        {
            FuelStationsModel FuelStationsModel_ = FuelStationsModel.GetFuelStationsBy_Id(id);
            ViewBag.thang = FuelStationsModel_.thang;
            return View(FuelStationsModel_);
        }
        [HttpPost]
        public ActionResult Fuel_Stations_Update(FuelStationsModel FuelStations)
        {
            if (ModelState.IsValid)
            {

                FuelStationsModel.updateFuelStationsDetail(FuelStations);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Fuel_Stations_Index", new { thang = FuelStations.thang });

            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(FuelStations);
        }
        
        //-------------------------Generator-------------------------------------------------------------------------

        FuelGeneratorModel FuelGeneratorModel = new FuelGeneratorModel();
        public ActionResult GeneratorIndex(string thang)
        {
            string nam = DateTime.Now.Year.ToString();
            string thang_="";
            if (thang.ToString().Length != 0)
                thang_ = thang;
            else
                thang_ = DateTime.Now.Month.ToString();
            TempData["select_thang"] = thang_;
            //List<FuelGeneratorAssociateModel> FuelGenerator = FuelGeneratorAssociateModel.getFuelGeneratorIndex(str_detail);
            List<FuelGeneratorModel> FuelGenerator = FuelGeneratorModel.getGeneratorIndex(nam, thang_);
            //List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            //ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            ViewBag.thang = thang;
            return View(FuelGenerator);
        }
        public ActionResult GeneratorDetailList(int main_stations_id, int thang)
        {
            string nam = DateTime.Now.Year.ToString();            
            List<FuelGeneratorModel> FuelGenerator = FuelGeneratorModel.getGeneratorDetailList(main_stations_id, nam, thang.ToString());
            ViewBag.thang = thang;
            ViewBag.main_stations_id = main_stations_id;
            return View(FuelGenerator);
        }
        public ActionResult DeleteGeneratorDetail(int id, int main_stations_id, int thang)
        {
            FuelGeneratorModel.deleteGeneratorDetail(id);
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("GeneratorDetailList", new { main_stations_id = main_stations_id, thang = thang });
        }
        [HttpPost]
        public ActionResult CheckedActionGenerator(int[] Items)
        {
            int main_stations_id = Convert.ToInt32(Request["main_stations_id"]);
            int thang = Convert.ToInt32(Request["thang"]);
            foreach (int item in Items)
            {
                FuelGeneratorModel.deleteGeneratorDetail(item);
            }
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("GeneratorDetailList", new { main_stations_id = main_stations_id, thang = thang });
        }

        public ActionResult GeneratorDetailEdit(int id, int main_stations_id, int thang)
        {
            int nam_ = Convert.ToInt32(DateTime.Now.Year.ToString());
            int thang_ = thang;
            FuelGeneratorModel FuelGeneratorModel_ = FuelGeneratorModel.getGeneratorDetailList(main_stations_id, nam_.ToString(), thang_.ToString()).Find(delegate(FuelGeneratorModel s) { return s.main_stations_id == main_stations_id; });
            return View(FuelGeneratorModel_);
        }
        [HttpPost]
        public ActionResult GeneratorDetailEdit(FuelGeneratorModel FuelGenerator)
        {
            if (ModelState.IsValid)
            {
                FuelGenerator.ngay_cap_nhat = DateTime.Today.ToString("dd/MM/yyyy");
                FuelGeneratorModel.updateGeneratorDetail(FuelGenerator);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("GeneratorDetailList", new { main_stations_id = FuelGenerator.main_stations_id, thang = FuelGenerator.thang });
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(FuelGenerator);
            //return RedirectToAction("GeneratorDetailEdit", new {id=FuelGenerator.id, main_stations_id = FuelGenerator.main_stations_id, thang = FuelGenerator.thang });
        }

        public ActionResult FuelGeneratorEdit(int id,int thang)
        {
            int nam_ = Convert.ToInt32(DateTime.Now.Year.ToString());
            int thang_ =  thang;
            //if (thang == 1)
            //{
            //    nam_ = nam_ - 1;
            //    thang_ = 12;    
            //}
            FuelGeneratorModel FuelGeneratorModel_ = FuelGeneratorModel.getGeneratorIndex(nam_.ToString(), thang_.ToString()).Find(delegate(FuelGeneratorModel s) { return s.main_stations_id == id; });
            FuelGeneratorModel_.nam = Convert.ToInt32(DateTime.Now.Year.ToString());
            FuelGeneratorModel_.thang = thang;
            return View(FuelGeneratorModel_);
        }
        [HttpPost]
        public ActionResult FuelGeneratorEdit(FuelGeneratorModel FuelGenerator)
        {            
            if (ModelState.IsValid)
            {              

                FuelGenerator.ngay_cap_nhat = DateTime.Today.ToString("dd/MM/yyyy");
                FuelGenerator.tong_tien = (Convert.ToDecimal(FuelGenerator.nhap_trong_thang) * Convert.ToDecimal(FuelGenerator.don_gia)).ToString();
                FuelGeneratorModel.updateFuelGenerator(FuelGenerator);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("GeneratorIndex", new { thang=FuelGenerator.thang});
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(FuelGenerator);
        }
        public ActionResult FindFuelGenerator()
        {
            return RedirectToAction("GeneratorIndex");
        }
        [HttpPost]
        public ActionResult FindFuelGenerator(string timkiem)
        {
            //string trungtam_id = "";
            //string tram_id = "";
            //int num;
            //bool main_stations_id_check = int.TryParse(trungtam_id, out num);
            //string s = "";
            //if (int.TryParse(Request.Form["main_stations_id"], out num))
            //{
            //    trungtam_id = Request.Form["main_stations_id"];
            //    s = " b.id=" + trungtam_id + " AND ";
            //}
            //if (int.TryParse(Request.Form["stations_id"], out num))
            //{
            //    tram_id = Request.Form["stations_id"];
            //    s = " c.id=" + tram_id + " AND ";
            //}
            //if (int.TryParse(Request.Form["main_stations_id"], out num) && int.TryParse(Request.Form["stations_id"], out num))
            //    s = " b.id=" + trungtam_id + " AND c.id=" + tram_id + " AND ";
            //if (Session != null && s.ToString() == "")
            //{
            //    int quyen_id = Convert.ToInt16(Session["quyen_id"]);
            //    int trungtam_session_id = Convert.ToInt16(Session["trungtam_id"]);
            //    if (quyen_id > 2)
            //        s = " b.id=" + trungtam_session_id + " AND ";
            //}

            //string sql;
            //string dk = "";
            //if (Request["select_option"] == "device_stations.name")
            //    dk = "a.name";
            //else if (Request["select_option"] == "device_stations.ma_he_thong")
            //    dk = "a.ma_he_thong";
            //if (timkiem == "")
            //    sql = s + "(" + dk + " like '%" + timkiem + "%' or " + dk + " is null)";
            //else
            //    sql = s + "(" + dk + " like '%" + timkiem + "%' or upper(" + dk + ") like upper('%" + timkiem + "%'))";
            //ViewBag.find_textbox = timkiem;
            //ViewBag.find_selectbox = Request["select_option"];

            int year = DateTime.Now.Year;
            string month = Request["thang_hidden"];
            //if (month.ToString() == "1")
            //{
            //    year = year - 1;
            //    month = "12";
            //}
            //else
            //    month = (Convert.ToInt32(month) - 1).ToString();
            List<FuelGeneratorModel> FuelGenerator = FuelGeneratorModel.getGeneratorIndex(year.ToString(), month.ToString());
            //List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            //ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            TempData["select_thang"] = Request["thang_hidden"];
            ViewBag.thang = Request["thang_hidden"];
            return View("GeneratorIndex", FuelGenerator);
        }
        //------------------------------Fuel For Generator----------------------------------------------------
        FuelGeneratorAssociateModel FuelGeneratorAssociateModel = new FuelGeneratorAssociateModel();
        public ActionResult Fuel_For_GeneratorIndex(string thang)
        {
            string nam = DateTime.Now.Year.ToString();
            string thang_ = "";
            if (thang.ToString().Length != 0)
                thang_ = thang;
            else
                thang_ = DateTime.Now.Month.ToString();
            TempData["select_thang"] = thang_;
            //List<FuelGeneratorAssociateModel> FuelGenerator = FuelGeneratorAssociateModel.getFuelGeneratorIndex(str_detail);
            List<FuelGeneratorAssociateModel> FuelGenerator = FuelGeneratorAssociateModel.getFuelGeneratorIndex(nam, thang_);
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            ViewBag.thang = thang;
            return View(FuelGenerator);
        }
        public ActionResult FindFuelGeneratoUse()
        {
            return RedirectToAction("Fuel_For_GeneratorIndex");
        }
        [HttpPost]
        public ActionResult FindFuelGeneratoUse(string timkiem)
        {
            string trungtam_id = "";
            string tram_id = "";
            int num;
            bool main_stations_id_check = int.TryParse(trungtam_id, out num);
            string s = "";
            //add by Namtd 09/03/2014
            ViewBag.selectedMain_stations = "";
            ViewBag.selectedStations = "";
            if (int.TryParse(Request.Form["main_stations_id"], out num))
            {
                trungtam_id = Request.Form["main_stations_id"];
                s = " b.id=" + trungtam_id + " AND ";
                //add by Namtd 09/03/2014
                ViewBag.selectedMain_stations = Request.Form["main_stations_id"];
            }
            if (int.TryParse(Request.Form["stations_id"], out num))
            {
                tram_id = Request.Form["stations_id"];
                s = " c.id=" + tram_id + " AND ";
                //add by Namtd 09/03/2014
                ViewBag.selectedStations = Request.Form["stations_id"];
            }
            if (int.TryParse(Request.Form["main_stations_id"], out num) && int.TryParse(Request.Form["stations_id"], out num))
                s = " b.id=" + trungtam_id + " AND c.id=" + tram_id + " AND ";
            if (Session != null && s.ToString() == "")
            {
                int quyen_id = Convert.ToInt16(Session["quyen_id"]);
                int trungtam_session_id = Convert.ToInt16(Session["trungtam_id"]);
                if (quyen_id > 2)
                    s = " b.id=" + trungtam_session_id + " AND ";
            }
            string sql;
            string dk = "";
            if (Request["select_option"] == "device_stations.name")
                dk = "a.name";
            else if (Request["select_option"] == "device_stations.ma_he_thong")
                dk = "a.ma_he_thong";
            if (timkiem == "")
                sql = s + "(" + dk + " like '%" + timkiem + "%' or " + dk + " is null)";
            else
                sql = s + "(" + dk + " like '%" + timkiem + "%' or upper(" + dk + ") like upper('%" + timkiem + "%'))";
            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];

            int year = DateTime.Now.Year;
            string month = Request["thang_hidden"];
            //if (month.ToString() == "1")
            //{
            //    year = year - 1;
            //    month = "12";
            //}
            //else
            //    month = (Convert.ToInt32(month) - 1).ToString();
            List<FuelGeneratorAssociateModel> FuelGenerator = FuelGeneratorAssociateModel.FindFuelGenerator(sql, year.ToString(), month.ToString());
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();

            //add by Namtd 09/03/2014
            if (ViewBag.selectedMain_stations.Length == 0)
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            else
            {
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name", ViewBag.selectedMain_stations);
                StationsModel StationsModel = new StationsModel();
                List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(ViewBag.selectedMain_stations));
                ViewBag.tram_id = new SelectList(stationModel, "id", "name", ViewBag.selectedStations);
            }

            TempData["select_thang"] = Request["thang_hidden"];
            ViewBag.thang = Request["thang_hidden"];
            return View("Fuel_For_GeneratorIndex", FuelGenerator);
        }
        public ActionResult FuelGeneratorUseList(int device_stations_id, int thang)
        {
            string nam = DateTime.Now.Year.ToString();
            List<FuelGeneratorAssociateModel> FuelGenerator = FuelGeneratorAssociateModel.getFuelGeneratorUseList(device_stations_id.ToString(), nam, thang.ToString());
            //List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            //ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            ViewBag.thang = thang;
            ViewBag.device_stations_id = device_stations_id;
            return View(FuelGenerator);
        }

        public ActionResult DeleteFuelGeneratorUse(int id, int device_stations_id, int thang)
        {
            FuelGeneratorAssociateModel.deleteFuelGeneratorUse(id);
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("FuelGeneratorUseList", new { device_stations_id = device_stations_id, thang = thang });
        }
        [HttpPost]
        public ActionResult CheckedActionGeneratorUse(int[] Items)
        {
            int device_stations_id = Convert.ToInt32(Request["device_stations_id"]);
            int thang = Convert.ToInt32(Request["thang"]);
            foreach (int item in Items)
            {
                FuelGeneratorAssociateModel.deleteFuelGeneratorUse(item);
            }
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("FuelGeneratorUseList", new { device_stations_id = device_stations_id, thang = thang });
        }

        public ActionResult AddFuelGeneratorUse(int device_stations_id, int thang)
        {
            int nam_ = Convert.ToInt32(DateTime.Now.Year.ToString());
            int thang_ = thang;
            FuelGeneratorAssociateModel FuelGeneratorModel_ = FuelGeneratorAssociateModel.getFuelGeneratorIndex(nam_.ToString(), thang_.ToString()).Find(delegate(FuelGeneratorAssociateModel s) { return s.device_stations_id == device_stations_id; });
            FuelGeneratorModel_.nam = Convert.ToInt32(DateTime.Now.Year.ToString());
            FuelGeneratorModel_.thang = thang;
            return View(FuelGeneratorModel_);
        }
        [HttpPost]
        public ActionResult AddFuelGeneratorUse(FuelGeneratorAssociateModel FuelGenerator)
        {
            if (Request["checkbox"] != null)
                FuelGenerator.fuel_status = 1;
            else
                FuelGenerator.fuel_status = 0;
            if (ModelState.IsValid)
            {
                FuelGenerator.ngay_cap_nhat = DateTime.Today.ToString("dd/MM/yyyy");
                FuelGeneratorAssociateModel.insertFuelGeneratorUse(FuelGenerator);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Fuel_For_GeneratorIndex", new { thang=FuelGenerator.thang});
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(FuelGenerator);
        }
        public ActionResult EditFuelGeneratorUse(int id, int device_stations_id, int thang)
        {
            int nam_ = Convert.ToInt32(DateTime.Now.Year.ToString());
            int thang_ = thang;
            FuelGeneratorAssociateModel FuelGeneratorModel_ = FuelGeneratorAssociateModel.getFuelGeneratorUseList(device_stations_id.ToString(),nam_.ToString(), thang_.ToString()).Find(delegate(FuelGeneratorAssociateModel s) { return s.id == id; });
            return View(FuelGeneratorModel_);
        }
        [HttpPost]
        public ActionResult EditFuelGeneratorUse(FuelGeneratorAssociateModel FuelGenerator)
        {
            if (ModelState.IsValid)
            {
                if (Request["checkbox"] != null)
                    FuelGenerator.fuel_status = 1;
                else
                    FuelGenerator.fuel_status = 0;
                FuelGenerator.ngay_cap_nhat = DateTime.Today.ToString("dd/MM/yyyy");
                FuelGeneratorAssociateModel.updateFuelGeneratorUse(FuelGenerator);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("FuelGeneratorUseList", new { device_stations_id = FuelGenerator.device_stations_id, thang = FuelGenerator.thang });
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(FuelGenerator);
        }



        //-----------------------------------------------------------------------------------------------------

        FuelGeneratorDetailModel FuelGeneratorDetailModel = new FuelGeneratorDetailModel();
        public ActionResult FuelGeneratorIndex(string thang)
        {
            string nam = DateTime.Now.Year.ToString();
            string thang_ = "";
            if (thang.ToString().Length != 0)
                thang_ = thang;
            else
                thang_ = DateTime.Now.Month.ToString();
            TempData["select_thang"] = thang_;
            List<FuelGeneratorDetailModel> FuelGenerator = FuelGeneratorDetailModel.getFuelGeneratorIndex(nam, thang_);
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            ViewBag.thang = thang;
            return View(FuelGenerator);
        }

        
        public ActionResult FuelGeneratorDetailEdit(int device_stations_id, int thang)
        {
            int nam_ = Convert.ToInt32(DateTime.Now.Year.ToString());
            int thang_ = thang;
            FuelGeneratorDetailModel FuelGeneratorModel_ = FuelGeneratorDetailModel.getFuelGeneratorIndex(nam_.ToString(), thang_.ToString()).Find(delegate(FuelGeneratorDetailModel s) { return s.device_stations_id == device_stations_id; });
            FuelGeneratorModel_.nam = Convert.ToInt32(DateTime.Now.Year.ToString());
            FuelGeneratorModel_.thang = thang;
            return View(FuelGeneratorModel_);
        }
        [HttpPost]
        public ActionResult FuelGeneratorDetailEdit(FuelGeneratorDetailModel FuelGenerator)
        {

            if (ModelState.IsValid)
            {
                FuelGenerator.ngay_cap_nhat = DateTime.Today.ToString("dd/MM/yyyy");
                FuelGeneratorDetailModel.insertFuelGeneratorDetail(FuelGenerator);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("FuelGeneratorIndex", new { thang=FuelGenerator.thang});
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(FuelGenerator);
        }



        public ActionResult FindFuelGeneratorDetail()
        {
            return RedirectToAction("FuelGeneratorIndex");
        }
        [HttpPost]
        public ActionResult FindFuelGeneratorDetail(string timkiem)
        {
            string trungtam_id = "";
            string tram_id = "";
            int num;
            bool main_stations_id_check = int.TryParse(trungtam_id, out num);
            string s = "";
            //add by Namtd 09/03/2014
            ViewBag.selectedMain_stations = "";
            ViewBag.selectedStations = "";
            if (int.TryParse(Request.Form["main_stations_id"], out num))
            {
                trungtam_id = Request.Form["main_stations_id"];
                s = " b.id=" + trungtam_id + " AND ";
                //add by Namtd 09/03/2014
                ViewBag.selectedMain_stations = Request.Form["main_stations_id"];
            }
            if (int.TryParse(Request.Form["stations_id"], out num))
            {
                tram_id = Request.Form["stations_id"];
                s = " c.id=" + tram_id + " AND ";
                //add by Namtd 09/03/2014
                ViewBag.selectedStations = Request.Form["stations_id"];
            }
            if (int.TryParse(Request.Form["main_stations_id"], out num) && int.TryParse(Request.Form["stations_id"], out num))
                s = " b.id=" + trungtam_id + " AND c.id=" + tram_id + " AND ";
            if (Session != null && s.ToString() == "")
            {
                int quyen_id = Convert.ToInt16(Session["quyen_id"]);
                int trungtam_session_id = Convert.ToInt16(Session["trungtam_id"]);
                if (quyen_id > 2)
                    s = " b.id=" + trungtam_session_id + " AND ";
            }

            string sql;
            string dk = "";
            if (Request["select_option"] == "device_stations.name")
                dk = "a.name";
            else if (Request["select_option"] == "device_stations.ma_he_thong")
                dk = "a.ma_he_thong";
            if (timkiem == "")
                sql = s + "(" + dk + " like '%" + timkiem + "%' or " + dk + " is null)";
            else
                sql = s + "(" + dk + " like '%" + timkiem + "%' or upper(" + dk + ") like upper('%" + timkiem + "%'))";
            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];

            int year = DateTime.Now.Year;
            string month = Request["thang_hidden"];
            //if (month.ToString() == "1")
            //{
            //    year = year - 1;
            //    month = "12";
            //}
            //else
            //    month = (Convert.ToInt32(month) - 1).ToString();
            List<FuelGeneratorDetailModel> FuelGenerator = FuelGeneratorDetailModel.FindFuelGenerator(sql, year.ToString(), month.ToString());
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();

            //add by Namtd 09/03/2014
            if (ViewBag.selectedMain_stations.Length == 0)
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            else
            {
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name", ViewBag.selectedMain_stations);
                StationsModel StationsModel = new StationsModel();
                List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(ViewBag.selectedMain_stations));
                ViewBag.tram_id = new SelectList(stationModel, "id", "name", ViewBag.selectedStations);
            }

            TempData["select_thang"] = Request["thang_hidden"];
            ViewBag.thang = Request["thang_hidden"];
            return View("FuelGeneratorIndex", FuelGenerator);
        }

        
        public ActionResult FuelStations_Report_Index()
        {
            return View("~/Views/Fuel/Report/FuelStations_Report_Index.cshtml");
        }
        public ActionResult FuelGenerator_Report_Index()
        {
            return View("~/Views/Fuel/Report/FuelGenerator_Report_Index.cshtml");
        }
        public ActionResult FuelGeneratorActivity_Report_Index()
        {
            //Session["GeneratorFuel"] = 2;
            //return View("~/Views/Fuel/Report/GeneratorActivityProgressive_Report_Index.cshtml");
            return View("~/Views/Fuel/Report/FuelGeneratorActivity_Report_Index.cshtml");
        }
        public ActionResult GeneratorActivityProgressive_Report_Index()
        {
            //Session["GeneratorFuel"]=1;
            return View("~/Views/Fuel/Report/GeneratorActivityProgressive_Report_Index.cshtml");
        }

        public ActionResult GeneratorLevel_Report_Index()
        {
            return View("~/Views/Fuel/Report/GeneratorLevel_Report_Index.cshtml");
        }

        //------------------------------Fuel Generator Detail List---------------------
        public ActionResult FuelGeneratorDetailList(int device_stations_id,int thang)
        {
            string nam = DateTime.Now.Year.ToString();
            List<FuelGeneratorDetailModel> FuelGenerator = FuelGeneratorDetailModel.getFuelGeneratorDetailList(device_stations_id.ToString(), nam, thang.ToString());
            //List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            //ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            ViewBag.thang = thang;
            ViewBag.device_stations_id = device_stations_id;
            return View(FuelGenerator);
        }
        public ActionResult DeleteFuelDetail(int id, int device_stations_id,int thang)
        {
            FuelGeneratorDetailModel.deleteFuelGeneratorDetail(id);
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("FuelGeneratorDetailList", new { device_stations_id = device_stations_id,thang = thang });
        }
        [HttpPost]
        public ActionResult CheckedAction(int[] Items)
        {
            int device_stations_id = Convert.ToInt32(Request["device_stations_id"]);
            int thang = Convert.ToInt32(Request["thang"]);
            foreach (int item in Items)
            {
                FuelGeneratorDetailModel.deleteFuelGeneratorDetail(item);
            }
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("FuelGeneratorDetailList", new { device_stations_id = device_stations_id, thang = thang });
        }

        public ActionResult FuelGeneratorDetailUpdate(int id, int device_stations_id, int thang)
        {
            int nam_ = Convert.ToInt32(DateTime.Now.Year.ToString());
            int thang_ = thang;
            FuelGeneratorDetailModel FuelGeneratorModel_ = FuelGeneratorDetailModel.getFuelGeneratorDetailList(device_stations_id.ToString(),nam_.ToString(), thang_.ToString()).Find(delegate(FuelGeneratorDetailModel s) { return s.id == id; });
            return View(FuelGeneratorModel_);
        }
        [HttpPost]
        public ActionResult FuelGeneratorDetailUpdate(FuelGeneratorDetailModel FuelGenerator)
        {
            if (ModelState.IsValid)
            {
                FuelGenerator.ngay_cap_nhat = DateTime.Today.ToString("dd/MM/yyyy");
                FuelGeneratorDetailModel.updateFuelGeneratorDetail(FuelGenerator);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("FuelGeneratorDetailList", new { device_stations_id = FuelGenerator.device_stations_id, thang = FuelGenerator.thang });
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            //return RedirectToAction("FuelGeneratorDetailList", new { device_stations_id = FuelGenerator.device_stations_id, thang = FuelGenerator.thang });
            return View(FuelGenerator);
        }
    }

}
